package be.kuleuven.cs.mop.domain.model.impl;


import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import be.kuleuven.cs.mop.domain.model.TaskType;
import be.kuleuven.cs.mop.domain.model.Theme;



/**
 * internal implementation of the {@link Theme} interface
 */
public class ThemeImpl implements Theme {
	
	private final Map<TaskTypeImpl, Map<String, String>>	fields	=
		new HashMap<TaskTypeImpl, Map<String, String>>();
	private final Map<String, ResourceTypeImpl> resources	=
		new TreeMap<String, ResourceTypeImpl>();
	private final Map<String, TaskTypeImpl>		tasks		=
		new TreeMap<String, TaskTypeImpl>();
	private final Map<String, UserTypeImpl>		users		=
		new TreeMap<String, UserTypeImpl>();
	
	
	
	@Override
	public String getFieldName(final TaskType task, final String id) {
		try {
			return fields.get((TaskTypeImpl)task).get(id);
		} catch (final NullPointerException e) {
			//	occurs if the TaskType has no defined fields
		}
		return null;
	}
	
	@Override
	public ResourceTypeImpl getResourceType(final String id) {
		final ResourceTypeImpl resource = resources.get(id);
		if (resource == null)
			throw new RuntimeException("ResourceType '" + id + "' does not exist");
		return resource;
	}
	
	@Override
	public TaskTypeImpl getTaskType(final String id) {
		final TaskTypeImpl task = tasks.get(id);
		if (task == null)
			throw new RuntimeException("TaskType '" + id + "' does not exist");
		return task;
	}
	
	@Override
	public UserTypeImpl getUserType(final String id) {
		final UserTypeImpl user = users.get(id);
		if (user == null)
			throw new RuntimeException("UserType '" + id + "' does not exist");
		return user;
	}
	
	public void setFieldNames(final TaskTypeImpl taskType,
			final Map<String, String> ids) {
//		Map<String, String> dictionary = fields.get(task);
//		if (dictionary == null)
//			fields.put(task, (dictionary = new TreeMap<String, String>()));
//		dictionary.put(id, name);
		fields.put(taskType, ids);
	}
	
	public void setResourceType(final String id, final ResourceTypeImpl type) {
		resources.put(id, type);
	}
	
	public void setTaskType(final String id, final TaskTypeImpl type) {
		tasks.put(id, type);
	}
	
	public void setUserType(final String id, final UserTypeImpl type) {
		users.put(id, type);
	}
	
}